/// <reference name="MicrosoftAjax.debug.js" />
/// <reference path="../../Resources/js/jquery/jquery.debug.js" />
/// <reference path="Addition.js" />

Type.registerNamespace("BM");

// ErrorLabelAddition object

BM.ErrorLabelAddition = function(leafItem, additionNode) {
    BM.ErrorLabelAddition.initializeBase(this, [leafItem, additionNode]);
}

BM.ErrorLabelAddition.prototype = {
    load: function() {
        for (var i = 0; i < this.getItem().getConstraints().length; i++) {
            var constraint = this.getItem().getConstraints()[i];
            constraint.addNotMet(Function.createDelegate(this, this.constraintNotMet));
            constraint.addMet(Function.createDelegate(this, this.clearErrorText));
        }
    },

    constraintNotMet: function(sender, args) {
        /// <summary>
        /// the method is fired when any of the constraints is not met
        /// </summary>
        /// <param name="sender" type="BM.Constraint" mayBeNull="false" >
        /// the 'notMet' event sender - the constraint
        /// </param>
        /// <param name="args" type="Array" >
        /// the event args
        /// </param>        
        this.updateErrorText(sender.getErrorText());
    },

    updateErrorText: function(text) {
        /// <summary>
        /// sets the error text on the error label
        /// </summary>
        /// <param name="text" type="String" mayBeNull="true" >
        /// the tet to update the error label with
        /// </param>        
        if (!text) return;
        var label = this.getErrorLabel();
        label.style.display = 'block';
        label.innerHTML = text;
    },

    clearErrorText: function() {
        /// <summary>
        /// clears the error text on the error label
        /// </summary>
        var label = this.getErrorLabel();
        label.style.display = 'none';
        label.innerHTML = '';
    },

    getErrorLabel: function() {
        ///	<summary>        
        /// returns the span that hold the error text
        ///	</summary>
        /// <returns domElement="true" >
        /// the error label
        /// </returns>
        if (this.errorLabel == null) {
            this.errorLabel = $(this.getItem().getNode()).find('.ErrorLabel')[0];
        }
        return this.errorLabel;
    }
}

BM.Addition['ErrorLabelAddition'] = BM.ErrorLabelAddition;
BM.ErrorLabelAddition.registerClass("BM.ErrorLabelAddition", BM.Addition);

